package com.yidu.BusinessData.service.impl;

import com.google.gson.Gson;
import com.yidu.BusinessData.entity.securityAccrued;
import com.yidu.BusinessData.mapper.SecurityAccruedMapper;
import com.yidu.BusinessData.service.SecurityAccruedService;
import com.yidu.CashManagement.util.CreateId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2022/12/13
 */
@Service
public class SecurityAccruedServiceImpl implements SecurityAccruedService {
    @Autowired
    private SecurityAccruedMapper securityAccruedMapper;

    /**
     * 查询遍历
     * @param page 当前页
     * @param row 行
     * @param businessTime 时间
     * @return
     */
    @Override
    public Map selectSecurityAccrued(int page, int row, String businessTime) {
        String term = append(businessTime).toString();
        Map selectMap = new HashMap();
        selectMap.put("page", (page - 1) * row);
        selectMap.put("row", row);
        selectMap.put("term", term);

        Map layUiMap = new HashMap();
        layUiMap.put("data", securityAccruedMapper.selectSecurityAccrued(selectMap));
        layUiMap.put("msg", "");
        layUiMap.put("code", 0);
        layUiMap.put("count", securityAccruedMapper.selectSecurityAccruedCont(selectMap));

        return layUiMap;
    }

    /**
     * 删除
     * @param securityAccrued
     * @return
     */
    @Override
    public String updateSecurityAccrued(securityAccrued securityAccrued) {
        String msg = "";
        try {
            int i = securityAccruedMapper.updateSecurityAccrued(securityAccrued);
            if(i>=1){
                msg="ok";
            }
        } catch (Exception e) {
            msg = "error";
        }
        return msg;
    }

    /**
     * 新增
     * @param securityAccrued
     * @return
     */
    @Override
    public int addSecurityAccrued(securityAccrued securityAccrued) {
        String id= CreateId.createId("CA");
        securityAccrued.setSecurityAccruedId(id);
        return securityAccruedMapper.addSecurityAccrued(securityAccrued);
    }

    /**
     * 删除
     * @param myArr
     */
    @Override
    public void deleteSecurityAccrued(String myArr) {
        String[] array = new Gson().fromJson(myArr, String[].class);
        for (String Id : array) {
            securityAccruedMapper.deleteSecurityAccrued(Id);

        }
    }

    /**
     * 查询最大id
     * @return
     */
    @Override
    public String maxSecurityAccruedId() {
        return securityAccruedMapper.maxSecurityAccruedId();
    }

    /**
     * 根据Id删除
     * @param securityAccrued
     * @return
     */
    @Override
    public int deleteSecurityAccrueds(securityAccrued securityAccrued) {
        return securityAccruedMapper.deleteSecurityAccrueds(securityAccrued);
    }

    /**
     * 两费查询
     * @param securityAccruedId
     * @return
     */
    @Override
    public int selectCashAccrueds(String securityAccruedId) {
        return securityAccruedMapper.selectCashAccrueds(securityAccruedId);
    }


    /**
     * 拼接条件
     * @param businessTime 时间
     */
    public StringBuffer append(String businessTime) {
        StringBuffer term = new StringBuffer();
        if (businessTime != null && !businessTime.equals("")) {
            term.append(" and businessTime='").append(businessTime).append("'");
        }
        return term;
    }


    /**
     * 查询指定日期的证券清算款
     *
     * @param time 查询日期
     * @return Double 清算款合计
     * @author 任俊杰
     */
    @Override
    public Double getLiquidationMoney(String time) {
        return securityAccruedMapper.getLiquidationMoney(time);
    }

}
